﻿Imports System.Data.SqlClient

Public Class frmProspectsCasesStatus

    Public SNo As Integer

    Sub FillProspectsLossReasons()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select Distinct Reason From ProspectsLossReasons", cnn)
            Dim Reader As SqlDataReader

            Me.CombLossReason.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.CombLossReason.Items.Add(Reader.Item(0))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub UpdateStatus()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand
            Dim Trans As SqlTransaction

            cnn.Open()
            Trans = cnn.BeginTransaction
            cmd.Connection = cnn
            cmd.Transaction = Trans

            cmd.CommandText = "Update ProspectCases Set Status=@Status Where SNo=" & SNo

            'Add values

            If RActive.Checked = True Then
                cmd.Parameters.AddWithValue("@Status", "Active")

            ElseIf RNotActive.Checked = True Then
                cmd.Parameters.AddWithValue("@Status", "Not Active")

            ElseIf RWon.Checked = True Then
                cmd.Parameters.AddWithValue("@Status", "Won")

            ElseIf RLost.Checked = True Then
                cmd.Parameters.AddWithValue("@Status", "Lost")

            ElseIf RNonDesired.Checked = True Then
                cmd.Parameters.AddWithValue("@Status", "Non Desired")
            End If

            cmd.ExecuteNonQuery()

            Trans.Commit()
            cnn.Close()

            If RActive.Checked = True Then
                Me.txtStatus.BackColor = Color.Yellow
                Me.txtStatus.Text = "Active"

            ElseIf RNotActive.Checked = True Then
                Me.txtStatus.BackColor = Color.Magenta
                Me.txtStatus.Text = "Not Active"

            ElseIf RWon.Checked = True Then
                Me.txtStatus.BackColor = Color.GreenYellow
                Me.txtStatus.Text = "Won"

            ElseIf RLost.Checked = True Then
                Me.txtStatus.BackColor = Color.Red
                Me.txtStatus.Text = "Lost"

            ElseIf RNonDesired.Checked = True Then
                Me.txtStatus.BackColor = Color.Silver
                Me.txtStatus.Text = "Non Desired"
            End If

            If Me.RLost.Checked = True Then
                Me.CombLossReason.Enabled = True

            ElseIf Me.RLost.Checked = False Then
                Me.CombLossReason.Enabled = False
                Me.CombLossReason.SelectedIndex = -1
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmProspectsCasesStatus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillProspectsLossReasons()

        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ProspectCases Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)

                Me.txtStatus.Text = Reader.Item("Status")

            End While
            cnn.Close()

            'Check status
            If Me.txtStatus.Text = "Active" Then
                Me.txtStatus.BackColor = Color.Yellow

            ElseIf Me.txtStatus.Text = "Won" Then
                Me.txtStatus.BackColor = Color.GreenYellow

            ElseIf Me.txtStatus.Text = "Lost" Then
                Me.txtStatus.BackColor = Color.Red

            ElseIf Me.txtStatus.Text = "Non Desired" Then
                Me.txtStatus.BackColor = Color.Silver
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        UpdateStatus()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New frmProspectLossReasons
        a.ShowDialog()
        FillProspectsLossReasons()
    End Sub
End Class